package com.beem.project.beem.ui;

import static com.google.android.apps.iosched.util.LogUtils.LOGD;
import static com.google.android.apps.iosched.util.LogUtils.LOGV;
import static com.google.android.apps.iosched.util.LogUtils.LOGW;
import static com.google.android.apps.iosched.util.LogUtils.makeLogTag;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;

import com.beem.project.beem.BeemApplication;
import com.beem.project.beem.R;
import com.beem.project.beem.service.Contact;
import com.beem.project.beem.service.PresenceAdapter;
import com.beem.project.beem.service.VVPacketAdapter;
import com.beem.project.beem.service.VVPacketFilterAdapter;
import com.beem.project.beem.service.VVPacketListenerAdapter;
import com.beem.project.beem.service.aidl.IBeemRosterListener;
import com.beem.project.beem.service.aidl.IChatManager;
import com.beem.project.beem.service.aidl.IRoster;
import com.beem.project.beem.service.aidl.IXmppFacade;
import com.beem.project.beem.ui.ContactList.VVContactsListener;
import com.beem.project.beem.ui.dialogs.builders.Alias;
import com.beem.project.beem.ui.dialogs.builders.ChatList;
import com.beem.project.beem.ui.dialogs.builders.DeleteContact;
import com.beem.project.beem.ui.dialogs.builders.ResendSubscription;
import com.beem.project.beem.utils.BeemBroadcastReceiver;
import com.beem.project.beem.utils.Status;
import com.btf.push.CityVCard;
import com.btf.push.CityVcardListener;
import com.btf.push.GPSPacket;
import com.btf.push.GPSPacketListener;
import com.btf.push.SchoolVCard;
import com.btf.push.SchoolVcardListener;
import com.btf.push.VcardListener;
import com.butterfly.vv.AlbumListUI;
import com.butterfly.vv.Grid2;
import com.butterfly.vv.VcardInfo;
import com.butterfly.vv.view.RemoteImageView;
import com.butterfly.vv.vv.utils.Debug;
import com.butterfly.vv.vv.utils.VVXMPPUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ContactExpandableList extends FragmentActivity implements
		OnScrollListener {
	// 瀛樻斁鐖跺垪琛ㄦ暟鎹�
	List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
	// 鏀惧瓙鍒楄〃鍒楄〃鏁版嵁
	List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
	ExpandableAdapter expandAdapter;
	ExpandableListView expandableList;
	private int indicatorGroupHeight;
	private int the_group_expand_position = -1;
	private int count_expand = 0;
	private Map<Integer, Integer> ids = new HashMap<Integer, Integer>();
	private LinearLayout view_flotage = null;
	private TextView group_content = null;
	private ImageView tubiao;
	
	//----------------------------begin--------------------------
	private static final Intent SERVICE_INTENT = new Intent();
    static {
	SERVICE_INTENT.setComponent(new ComponentName("com.beem.project.beem", "com.beem.project.beem.BeemService"));
    }

    private static final String TAG = makeLogTag(ContactList.class);
    private static final float PAGER_TAB_SECONDARY_ALPHA = 0.5f;
    private final List<String> mListGroup = new ArrayList<String>();

    /** Map containing a list of the different contacts of a given group.
     * Each list is a @{link SortedList} so there is no need to sort it again.
     * */
    private final Map<String, List<Contact>> mContactOnGroup = new HashMap<String, List<Contact>>();
    private final ServiceConnection mServConn = new BeemServiceConnection();
    private final BeemBroadcastReceiver mReceiver = new BeemBroadcastReceiver();
    private final Map<String, ContactListAdapter> contactListAdapters = new HashMap<String, ContactListAdapter>();

    private final BeemRosterListener mBeemRosterListener = new BeemRosterListener();
    private IRoster mRoster;
    private IXmppFacade mXmppFacade;
    private IChatManager mChatManager;
    private SharedPreferences mSettings;
    private boolean mBinded;
    private ViewPager viewPager;
    private ListPagerAdapter groupsPagesAdapter;
    private PagerTabStrip pagerTabs;
    
   // ImageView imageView ;
    RemoteImageView imageView ;
    TextView chat_contact_name;
    Bitmap myAvatar;
    VCard myCard;
    Contact myContact;
    
    private Spinner mSpinner;
    private ArrayAdapter<CharSequence> mAdapter;
    
    //20121221
    Handler vvHandler = new Handler(){
    	@Override
    	public void handleMessage(Message msg) {
    		// TODO Auto-generated method stub
    		super.handleMessage(msg);
    		Bundle data = msg.getData();
    		ArrayList<com.teleca.jamendo.api.Contact> contacts = new ArrayList<com.teleca.jamendo.api.Contact>();
    		
    		switch (msg.what) {
			case 1:
				ArrayList<Contact> arrayList = data.getParcelableArrayList("gpsitems");
	    		for (Iterator iterator = arrayList.iterator(); iterator.hasNext();) {
					Contact contact = (Contact) iterator.next();
					com.teleca.jamendo.api.Contact c = new com.teleca.jamendo.api.Contact();
					c.setmName(contact.getJID());
					c.setDistance(contact.getDistance());
					contacts.add(c);
					//Debug.getDebugInstance().log("55 "+contact.getJID()+contact.getDistance());
				}
				break;
			case 2:
				Debug.getDebugInstance().log("school msg");
				ArrayList<Contact> scharrayList = data.getParcelableArrayList("schoolvvcontacts");
				for (Iterator iterator2 = scharrayList.iterator(); iterator2
						.hasNext();) {
					Contact contact = (Contact) iterator2.next();
					com.teleca.jamendo.api.Contact c = new com.teleca.jamendo.api.Contact();
					c.setmName(contact.getJID());
					c.setDistance(contact.getDistance());
					contacts.add(c);
					Debug.getDebugInstance().log("msg2 "+contact.getName());
				}
				//startActivity(new Intent(ContactList.this, Grid2.class).putExtra("contacts", contacts));
				break;
			case 3:
				
				ArrayList<Contact> cityarrayList = data.getParcelableArrayList("cityvvcontacts");
				for (Iterator iterator2 = cityarrayList.iterator(); iterator2
						.hasNext();) {
					Contact contact = (Contact) iterator2.next();
					com.teleca.jamendo.api.Contact c = new com.teleca.jamendo.api.Contact();
					c.setmName(contact.getJID());
					c.setDistance(contact.getDistance());
					contacts.add(c);
					Debug.getDebugInstance().log("msg3 "+contact.getName());
				}
				//startActivity(new Intent(ContactList.this, Grid2.class).putExtra("contacts", contacts));
				
				break;
			case 4:
				chat_contact_name.setText(data.getString("myname")==null?"未命名":data.getString("myname"));
				Bitmap bitmap = null;
				if (data.getByteArray("avatar")!=null) {
					 bitmap = BitmapFactory.decodeByteArray(data.getByteArray("avatar"),0,data.getByteArray("avatar").length);

				}
				if (bitmap != null) {
					imageView.setImageBitmap(bitmap);
				} else {
					imageView.setImageResource(R.drawable.no_avatar);
				}
				;
				break;

			default:
				break;
			}
    		//Debug.getDebugInstance().log("dd "+data.getParcelableArrayList("gpsitems"));
    		
    		//20130103startActivity(new Intent(ContactList.this, Grid2.class).putExtra("contacts", contacts));
    		//Debug.getDebugInstance().log(getClass().getName()+"===="+" "+msg.what);
    	}
    };
    
    VVPacketFilterAdapter vvfilter = new VVPacketFilterAdapter(new PacketTypeFilter(GPSPacket.class));
    //VVPacketFilterAdapter vvfilter = new VVPacketFilterAdapter(new PacketTypeFilter(Packet.class));
    VVPacketListenerAdapter vvpacketListener = new VVPacketListenerAdapter(new GPSPacketListener(vvHandler));
    
    VVPacketFilterAdapter schoolfilter = new VVPacketFilterAdapter(new PacketTypeFilter(SchoolVCard.class));
    VVPacketListenerAdapter schoolListenerAdapter = new VVPacketListenerAdapter(new SchoolVcardListener(vvHandler));
    
    VVPacketFilterAdapter cityfilter = new VVPacketFilterAdapter(new PacketTypeFilter(CityVCard.class));
    VVPacketListenerAdapter cityListenerAdapter = new VVPacketListenerAdapter(new CityVcardListener(vvHandler));
    
    VVPacketFilterAdapter vcardfilter = new VVPacketFilterAdapter(new PacketTypeFilter(VCard.class));
    VVPacketListenerAdapter vcardListenerAdapter = new VVPacketListenerAdapter(new VcardListener(vvHandler));
    
    //20121221

    /**
     * Constructor.
     */
//    public ContactList() {
//    }

    /** 取消menu20130131
     * Callback for menu creation.
     * @param menu the menu created
     * @return true on success, false otherwise
     
    @Override
    public final boolean onCreateOptionsMenu(Menu menu) {
	super.onCreateOptionsMenu(menu);
	MenuInflater inflater = getMenuInflater();
	inflater.inflate(R.menu.contact_list, menu);
	return true;
    }
    */
    
    //20130104
    @Override
    protected Dialog onCreateDialog(int id) {
    	// TODO Auto-generated method stub
    	if (id == SELECT_PHOTO_DLG)
			return createPickPhotoDialog();
		return null;
    }
    
    private Dialog createPickPhotoDialog() {
		// Wrap our context to inflate list items using correct theme
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);

		final ListAdapter adapter = ArrayAdapter.createFromResource(
				dialogContext, R.array.pick_stranger_items,
				android.R.layout.simple_list_item_1);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle("分类查找");
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						switch (which) {
						case 0:
							//doTakePhoto();
							strartGpsSearchUI(new GPSPacket());
							break;
						case 1:
							//doPickPhotoFromGallery();
							SchoolVCard schoolVCard = new SchoolVCard();
							schoolVCard.setSchoolname("6666");
							schoolVCard.setEntratime("2009");
//							com.btf.push.SchoolVCard.Item item = new com.btf.push.SchoolVCard.Item("jid", "i");
//							schoolVCard.addRosterItem(item);
							try {
								mXmppFacade.conllectVVPacket(new VVPacketAdapter(schoolVCard));
							} catch (RemoteException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
						case 2:
							CityVCard cityVCard = new CityVCard();
							cityVCard.setFromCity("0725");
							
							try {
								mXmppFacade.sendVVPacket(new VVPacketAdapter(cityVCard));
								//mXmppFacade.conllectVVPacket(new VVPacketAdapter(cityVCard));
							} catch (RemoteException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							break;
						default:
							Log.w(TAG,
									"DialogInterface onClick : invalid which code");
						}
					}
				});
		return builder.create();
	}

    private static final int SELECT_PHOTO_DLG = 0;
    //20130104

	private void strartGpsSearchUI(Packet packet) {
		try {
			//20120103mXmppFacade.sendVVPacket(new VVPacketAdapter(new GPSPacket()));
			//VVPacketAdapter vvPacketAdapter = mXmppFacade.conllectVVPacket(new VVPacketAdapter(new GPSPacket()));
			VVPacketAdapter vvPacketAdapter = mXmppFacade.conllectVVPacket(new VVPacketAdapter(packet));
			if (vvPacketAdapter.getPacket()!=null) {
				//Debug.getDebugInstance().log("-->"+vvPacketAdapter.getPacket().toXML());
				GPSPacket gpsPacket = (GPSPacket) vvPacketAdapter.getPacket();
				if (gpsPacket.getType().equals(Type.RESULT)) {
					Collection<com.btf.push.GPSPacket.Item> items = gpsPacket.getRosterItems();
					//ArrayList<com.beem.project.beem.service.Contact> contacts = new ArrayList<com.beem.project.beem.service.Contact>();
					ArrayList<com.teleca.jamendo.api.Contact> contacts = new ArrayList<com.teleca.jamendo.api.Contact>();
					for (Iterator iterator = items.iterator(); iterator.hasNext();) {
						com.btf.push.GPSPacket.Item itemgps = (com.btf.push.GPSPacket.Item) iterator.next();
						//com.beem.project.beem.service.Contact contact = new com.beem.project.beem.service.Contact(itemgps.getUser());
						com.teleca.jamendo.api.Contact contact = new com.teleca.jamendo.api.Contact();
						contact.setmName(itemgps.getUser());
						contact.setDistance(itemgps.getDistance());
						contacts.add(contact);
					}
//					startActivity(new Intent(ContactList.this, Grid2.class).putExtra("contacts", contacts));
					//startActivity(new Intent(ContactList.this, SearchUserTabs.class).putExtra("contacts", contacts));
					
				}
			}
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

    
    private void modifyMyVcardClick(){
    	Intent myintent = new Intent(this, VcardInfo.class);
    	myintent.putExtra("myavatar", myContact);
    	startActivity(myintent);
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	 //getMyStatus();
//    	imageView.setImageLevel(getMyStatus());
    	try {
    		//addVVPacketListener();
			//Debug.getDebugInstance().log(getClass().getName()+".onResume:"+mXmppFacade.getUserInfo().getJid());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	private int getMyStatus() {
		String status = mSettings.getString(BeemApplication.STATUS_TEXT_KEY, "");
		 int mode = mSettings.getInt(BeemApplication.STATUS_KEY, 0);
		 Presence pres = new Presence(Presence.Type.available);
		 Presence.Mode modep = Status.getPresenceModeFromStatus(mode);
		 pres.setMode(modep);
		 Log.i("VV", mode+" mode ---- status "+status+"*****"+Status.getStatusFromPresence(pres));
		 return Status.getStatusFromPresence(pres);
		//return mode*100;
	}
    
	private void addVVPacketListener(VVPacketListenerAdapter listenerAdapter,VVPacketFilterAdapter filterAdapter) {
		try {
			//mXmppFacade.addVVPacketListener(vvpacketListener, vvfilter);
			mXmppFacade.addVVPacketListener(listenerAdapter, filterAdapter);
		} catch (RemoteException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
    
    //20121209
    public void contactClickHandler(com.teleca.jamendo.api.Contact contact,IRoster iRoster) {
    	//AlbumListUI.launch(,contact);
    	Log.i("tag","  hh "+ contact.getmID());
		AlbumListUI.launch(this, contact,iRoster);
    }
    //20121209
    

    @Override
    protected void onStart() {
	super.onStart();
	Debug.getDebugInstance().log(getClass().getName()+".onStart");
//	if (!mSettings.getBoolean(BeemApplication.HIDE_GROUPS_KEY, false))
//	    showGroups();
//	else
//	    hideGroups();

	if (!mBinded)
		Debug.getDebugInstance().log(getClass().getName()+".mBinded "+mBinded);
	    mBinded = bindService(SERVICE_INTENT, mServConn, BIND_AUTO_CREATE);
		//toServiceConnected();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onPause() {
	super.onPause();
	try {
	    if (mRoster != null) {
		mRoster.removeRosterListener(mBeemRosterListener);
		mRoster = null;
	    }
	} catch (RemoteException e) {
	    LOGD("ContactList", "Remote exception", e);
	}
	if (mBinded) {
	    unbindService(mServConn);
		//toServiceDisconnected();
	    mBinded = false;
	}
	
	//20121221
	removeVVPacketListener(vvpacketListener);
	removeVVPacketListener(schoolListenerAdapter);
	removeVVPacketListener(cityListenerAdapter);
	removeVVPacketListener(vcardListenerAdapter);//20130125
//		try {
//			if (mXmppFacade != null) {
//				mXmppFacade.removeVVPacketListener(vvpacketListener);
//			}
//			Debug.getDebugInstance().log("onPause removeVVPacketListener"+getClass().getName());
//		} catch (RemoteException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	//20121221
	
	mXmppFacade = null;
    }

    @Override
    protected void onDestroy() {
	super.onDestroy();
	this.unregisterReceiver(mReceiver);
	LOGV(TAG, "onDestroy activity");
    }

    /**
     * Get a {@link ContactListAdapter} for a group.
     * The {@link ContactListAdapter} will be created if it is not exist.
     * @param group the group
     * @return the adapter
     */
    ContactListAdapter getContactListAdapter(String group) {
	synchronized (contactListAdapters) {
	    ContactListAdapter contactListAdapter = contactListAdapters.get(group);
	    if (contactListAdapter == null) {
	    /*20120125ori
		contactListAdapter = new ContactListAdapter(ContactList.this);
		*/
	    //20120125
	    	contactListAdapter = new ContactListAdapter(ContactExpandableList.this, mXmppFacade);
	    //20120125
	    	
		//20121218
		if (mXmppFacade != null) {
			try {
				
				contactListAdapter.setmUserInfo(mXmppFacade.getUserInfo());
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		//20121218
		
		contactListAdapters.put(group, contactListAdapter);
		List<String> realGroups = mListGroup.subList(1, mListGroup.size() - 1);
		if (!mListGroup.contains(group)) {
		    boolean added = false;
		    // insert group in sorted list
		    for (ListIterator<String> iterator = realGroups.listIterator(); iterator.hasNext();) {
			String currentGroup = (String) iterator.next();
			if (currentGroup.compareTo(group) > 0) {
			    iterator.previous();
			    iterator.add(group);
			    added = true;
			    break;
			}
		    }
		    if (!added)
			realGroups.add(group);
		    groupsPagesAdapter.notifyDataSetChanged();
		}
	    }
	    boolean hideDisconnected = mSettings.getBoolean(BeemApplication.SHOW_OFFLINE_CONTACTS_KEY, false);
	    contactListAdapter.setOnlineOnly(hideDisconnected);
	    return contactListAdapter;
	}
    }

    /**
     * Exectute a context menu action on a specified contact.
     * @param itemId the id of the menu action
     * @param contact the contact
     */
    void doContextMenuAction(int itemId, Contact contact) {
	switch (itemId) {
	    case R.id.contact_list_context_menu_call_item:
		try {
		    mXmppFacade.call(contact.getJID() + "/psi");
		} catch (RemoteException e) {
		    e.printStackTrace();
		}
		break;
	    case R.id.contact_list_context_menu_userinfo_alias:
//		Dialog alias = new Alias(ContactExpandableList.this, mRoster, contact).create();
//		alias.show();
		break;
	    case R.id.contact_list_context_menu_userinfo_subscription:
		Dialog subscription = new ResendSubscription(ContactExpandableList.this,
			mXmppFacade, contact).create();
		subscription.show();
		break;
	    case R.id.contact_list_context_menu_userinfo_delete:
		Dialog delete = new DeleteContact(ContactExpandableList.this, mRoster, contact).create();
		delete.show();
		break;
	    default:
		LOGW(TAG, "Context menu action not supported" + itemId);
		break;
	}

    }

    /**
     * Show the groups view.
     */
    private void showGroups() {
	pagerTabs.setVisibility(View.VISIBLE);
    }

    /**
     * Hide the groups view.
     */
    private void hideGroups() {
	pagerTabs.setVisibility(View.GONE);
    }

    /**
     * Remove old groups on the banner.
     * @throws RemoteException if an error occur when communicating with the service
     */
    private void cleanBannerGroup() throws RemoteException {
	if (mListGroup.size() <= 2)
	    return;
	List<String> rosterGroups = mRoster.getGroupsNames();
	Collections.sort(rosterGroups);
	List<String> realGroups = mListGroup.subList(1, mListGroup.size() - 1);
	realGroups.clear();
	realGroups.addAll(rosterGroups);
	groupsPagesAdapter.notifyDataSetChanged();
    }

    /**
     * Add a contact to the special list No Group and All contacts.
     * The contact will be added if the list is not the current list otherwise
     * the list must be modified in a Handler.
     *
     * @param contact the contact to add.
     */
    private void addToSpecialList(Contact contact) {
	List<String> groups = contact.getGroups();

	ContactListAdapter adapter = getContactListAdapter(getString(R.string.contact_list_all_contact));
	adapter.put(contact);
	if (groups.isEmpty()) {
	    adapter = getContactListAdapter(getString(R.string.contact_list_no_group));
	    adapter.put(contact);
	}
    }

	private void removeVVPacketListener(VVPacketListenerAdapter listenerAdapter) {
		try {
			if (mXmppFacade!=null) {
				//mXmppFacade.removeVVPacketListener(vvpacketListener);
				mXmppFacade.removeVVPacketListener(listenerAdapter);
			}
			Debug.getDebugInstance().log("onServiceDisconnected removeVVPacketListener"+getClass().getName());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
     * Listener on service event.
     */
    private class BeemRosterListener extends IBeemRosterListener.Stub {
	/**
	 * Constructor.
	 */
	public BeemRosterListener() {
	}

	/**
	 * {@inheritDoc}
	 * Simple stategy to handle the onEntriesAdded event.
	 * if contact has to be shown :
	 * <ul>
	 * <li> add him to his groups</li>
	 * <li> add him to the specials groups</>
	 * </ul>
	 */
	@Override
	public void onEntriesAdded(final List<String> addresses) throws RemoteException {
	    for (String newName : addresses) {
	    	final Contact contact = mRoster.getContact(StringUtils.parseBareAddress(newName));
	    	putContactInList(contact);
	    }
	}

	/**
	 * {@inheritDoc}
	 * Simple stategy to handle the onEntriesDeleted event.
	 * <ul>
	 * <li> Remove the contact from all groups</li>
	 * </ul>
	 */
	@Override
	public void onEntriesDeleted(final List<String> addresses) throws RemoteException {
	    LOGD(TAG, "onEntries deleted " + addresses);
	    for (String cToDelete : addresses) {
		final Contact contact = new Contact(cToDelete);
		for (final ContactListAdapter adapter : contactListAdapters.values()) {
		    runOnUiThread(new Runnable() {

			@Override
			public void run() {
			    adapter.remove(contact);
			}
		    });
		}
	    }
	    cleanBannerGroup();

	}

	/**
	 * {@inheritDoc}
	 * Simple stategy to handle the onEntriesUpdated event.
	 * <ul>
	 * <li> Remove the contact from all groups</li>
	 * <li> if contact has to be shown add it to his groups</li>
	 * <li> if contact has to be shown add it to the specials groups</li>
	 * </ul>
	 */
	@Override
	public void onEntriesUpdated(final List<String> addresses) throws RemoteException {
	    LOGD(TAG, "onEntries updated " + addresses);
	    for (String cToDelete : addresses) {
		Contact contact = new Contact(cToDelete);
		for (ContactListAdapter adapter : contactListAdapters.values()) {
		    adapter.remove(contact);
		}
	    }
	    for (String newName : addresses) {
		final Contact contact = mRoster.getContact(StringUtils.parseBareAddress(newName));
		putContactInList(contact);
	    }
	    cleanBannerGroup();
	}

	/**
	 * {@inheritDoc}
	 * Simple stategy to handle the onPresenceChanged event.
	 * <ul>
	 * <li> Remove the contact from all groups</li>
	 * <li> if contact has to be shown add it to his groups</li>
	 * <li> if contact has to be shown add it to the specials groups</li>
	 * </ul>
	 */
	@Override
	public void onPresenceChanged(PresenceAdapter presence) throws RemoteException {
	    String from = presence.getFrom();
	    final Contact contact = mRoster.getContact(StringUtils.parseBareAddress(from));
	    putContactInList(contact);
	}

	/**
	 * Put a contact in the different group list.
	 * @param contact the contact
	 */
	private void putContactInList(final Contact contact) {
	    List<String> groups = contact.getGroups();
	    for (final String group : groups) {
		runOnUiThread(new Runnable() {

		    @Override
		    public void run() {
			ContactListAdapter contactListAdapter = getContactListAdapter(group);
			contactListAdapter.put(contact);
		    }
		});
	    }

	    runOnUiThread(new Runnable() {

		@Override
		public void run() {
		    addToSpecialList(contact);
		}
	    });
	}
    }
    
    public void toServiceConnected() {
	    mXmppFacade = ContactListSessionSearchTab.getmXmppFacade();
	    
	    //20121221
	    Debug.getDebugInstance().log(getClass().getName()+"--------");
	    addVVPacketListener(vvpacketListener, vvfilter);
	    addVVPacketListener(schoolListenerAdapter, schoolfilter);
	    addVVPacketListener(cityListenerAdapter, cityfilter);
	    addVVPacketListener(vcardListenerAdapter, vcardfilter);//20120125
	    
	    //20121221
	    
	    try {
	    	//20130125
	    	 VCard vCard = new VCard();
			 vCard.setTo("dd@192.168.12.241");
			
			 //20130125
		mRoster = mXmppFacade.getRoster();
		if (mRoster != null) {
		    List<String> tmpGroupList = mRoster.getGroupsNames();
		    cleanBannerGroup();
		    synchronized (contactListAdapters) {
			for (ContactListAdapter ca : contactListAdapters.values()) {
			    ca.clear();
			}
		    }
		    Log.i("VV",mRoster.getContactList().get(0)+"+++++++"+ mRoster.getContactList().size());
		    toassignContactToGroups(mRoster.getContactList(), tmpGroupList);
		    //Log.i("VV", "000 "+mRoster.getContactList().size());
		    //20130125
		    /*
		    List<Contact> filterList = new ArrayList<Contact>();
		    int k= 0;
		    for (Iterator iterator = mRoster.getContactList().iterator(); iterator
					.hasNext();) {
		    	//Contact contact = mRoster.getContactList().get(k);
		    	Contact contact = (Contact) iterator.next();
		    	VCard photo = new VCard();
		    	//VCard card = (VCard) mXmppFacade.conllectVVPacket(new VVPacketAdapter(photo)).getPacket();
		    	mXmppFacade.load(contact.getJID());
		    	//contact.setPhoto(StringUtils.encodeBase64(card.getAvatar()));
		    	//if(card!=null){
		    		Log.i("VV", "-=-=-"+k+" ** ");
		    	//mRoster.getContactList().get(k).setPhoto(StringUtils.encodeBase64(card.getAvatar()));
		    	//}
		    	//filterList.add(contact);
				k++;
			}
		    assignContactToGroups(mRoster.getContactList(), tmpGroupList);
		    */
		    //20130125
		    
		    mRoster.addRosterListener(mBeemRosterListener);
		    LOGD(TAG, "add roster listener");
		    mChatManager = mXmppFacade.getChatManager();
		}
	    } catch (RemoteException e) {
		e.printStackTrace();
	    }
	}
    
    public void toServiceDisconnected() {
	    try {
		mRoster.removeRosterListener(mBeemRosterListener);
	    } catch (RemoteException e) {
		e.printStackTrace();
	    }
	    
	  //20121221
	  		removeVVPacketListener(vvpacketListener);
	  		removeVVPacketListener(schoolListenerAdapter);
	  		removeVVPacketListener(cityListenerAdapter);
	  		removeVVPacketListener(vcardListenerAdapter);//20120125
	  	//20121221
	    
	    mXmppFacade = null;
	    mChatManager = null;
	    mRoster = null;
	    mListGroup.clear();
	    mContactOnGroup.clear();
	    mBinded = false;

	}
    
    private void toassignContactToGroups(List<Contact> contacts, List<String> groupNames) {
	    for (Contact c : contacts) {
		addToSpecialList(c);

		List<String> groups = c.getGroups();

		for (String currentGroup : groups) {
		    ContactListAdapter cl = getContactListAdapter(currentGroup);
		    cl.put(c);
		}
	    }
	}


    /**
     * The service connection used to connect to the Beem service.
     */
    private class BeemServiceConnection implements ServiceConnection {

	/**
	 * Constructor.
	 */
	public BeemServiceConnection() {
	}

	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
	    mXmppFacade = IXmppFacade.Stub.asInterface(service);
	    
	    //20121221
	    Debug.getDebugInstance().log(getClass().getName()+"--------");
	    addVVPacketListener(vvpacketListener, vvfilter);
	    addVVPacketListener(schoolListenerAdapter, schoolfilter);
	    addVVPacketListener(cityListenerAdapter, cityfilter);
	    addVVPacketListener(vcardListenerAdapter, vcardfilter);//20120125
	    
	    //20121221
	    
	    try {
	    	//20130125
	    	 VCard vCard = new VCard();
			 vCard.setTo("dd@192.168.12.241");
			//mXmppFacade.conllectVVPacket(new VVPacketAdapter(vCard)).getPacket();
	    	//20120125mXmppFacade.load(null);//测试
			 //new DownloadMyAvatarTask(imageView).execute(mXmppFacade.getUserInfo().getJid());//20130129
//			 imageView.setXmppFacade(mXmppFacade);
//			 imageView.setImageUrl(VVXMPPUtils.makeVVXmppJid(mXmppFacade.getUserInfo().getJid()));//20130129
			 //imageView.setImageLevel(getMyStatus());
			 //imageView.setImageLevel(curContact.getStatus());
			 //20130125
		mRoster = mXmppFacade.getRoster();
		if (mRoster != null) {
		    List<String> tmpGroupList = mRoster.getGroupsNames();
		    cleanBannerGroup();
		    synchronized (contactListAdapters) {
			for (ContactListAdapter ca : contactListAdapters.values()) {
			    ca.clear();
			}
		    }
		    Log.i("VV",mRoster.getContactList().get(0)+"+++++++"+ mRoster.getContactList().size());
		    assignContactToGroups(mRoster.getContactList(), tmpGroupList);
		    //Log.i("VV", "000 "+mRoster.getContactList().size());
		    //20130125
		    /*
		    List<Contact> filterList = new ArrayList<Contact>();
		    int k= 0;
		    for (Iterator iterator = mRoster.getContactList().iterator(); iterator
					.hasNext();) {
		    	//Contact contact = mRoster.getContactList().get(k);
		    	Contact contact = (Contact) iterator.next();
		    	VCard photo = new VCard();
		    	//VCard card = (VCard) mXmppFacade.conllectVVPacket(new VVPacketAdapter(photo)).getPacket();
		    	mXmppFacade.load(contact.getJID());
		    	//contact.setPhoto(StringUtils.encodeBase64(card.getAvatar()));
		    	//if(card!=null){
		    		Log.i("VV", "-=-=-"+k+" ** ");
		    	//mRoster.getContactList().get(k).setPhoto(StringUtils.encodeBase64(card.getAvatar()));
		    	//}
		    	//filterList.add(contact);
				k++;
			}
		    assignContactToGroups(mRoster.getContactList(), tmpGroupList);
		    */
		    //20130125
		    
		    mRoster.addRosterListener(mBeemRosterListener);
		    LOGD(TAG, "add roster listener");
		    mChatManager = mXmppFacade.getChatManager();
		}
	    } catch (RemoteException e) {
		e.printStackTrace();
	    }
	}



	@Override
	public void onServiceDisconnected(ComponentName name) {
	    try {
		mRoster.removeRosterListener(mBeemRosterListener);
	    } catch (RemoteException e) {
		e.printStackTrace();
	    }
	    
	  //20121221
	  		removeVVPacketListener(vvpacketListener);
	  		removeVVPacketListener(schoolListenerAdapter);
	  		removeVVPacketListener(cityListenerAdapter);
	  		removeVVPacketListener(vcardListenerAdapter);//20120125
	  	//20121221
	    
	    mXmppFacade = null;
	    mChatManager = null;
	    mRoster = null;
	    mListGroup.clear();
	    mContactOnGroup.clear();
	    mBinded = false;

	}

	/**
	 * Assign the differents contact to their groups.
	 * This methods will fill the mContactOnGroup map.
	 *
	 * @param contacts list of contacts
	 * @param groupNames list of existing groups
	 */
	private void assignContactToGroups(List<Contact> contacts, List<String> groupNames) {
	    for (Contact c : contacts) {
		addToSpecialList(c);

		List<String> groups = c.getGroups();

		for (String currentGroup : groups) {
		    ContactListAdapter cl = getContactListAdapter(currentGroup);
		    cl.put(c);
		}
	    }
	}

    }

    /**
     * PagerAdapter for the contact list.
     */
    private class ListPagerAdapter extends FragmentPagerAdapter {

	/**
	 * Create a {@link ListPagerAdapter}.
	 * @param fm the {@link FragmentManager}
	 * @param viewPager the {@link ViewPager} associate with this adapter
	 */
	public ListPagerAdapter(final FragmentManager fm, final ViewPager viewPager) {
	    super(fm);
	    viewPager.setAdapter(this);
	}
	public ListPagerAdapter(final FragmentManager fm) {
	    super(fm);
	    
	}

	@Override
	public Fragment getItem(int position) {
	    String group = mListGroup.get(position);
	    ContactListFragment f = ContactListFragment.newInstance(group);
	    f.setListAdapter(getContactListAdapter(group));
	    return f;
	}

	@Override
	public int getCount() {
	    return mListGroup.size();
	}

	@Override
	public String getPageTitle(int position) {
	    return mListGroup.get(position);
	}

    }

    //20120125
    class DownloadMyAvatarTask extends AsyncTask<String, Void, Packet>{
    	ImageView img;
		public DownloadMyAvatarTask(ImageView img) {
			// TODO Auto-generated constructor stub
			this.img = img;
		}
    	
    	@Override
		protected Packet doInBackground(String... params) {
			// TODO Auto-generated method stub
			VCard result= null;
			if (mXmppFacade != null) {
				VCard card = new VCard();
				Log.i("VV",VVXMPPUtils.makeVVXmppJid(params[0])+ " my jid "+params[0]);
				card.setTo(VVXMPPUtils.makeVVXmppJid(params[0]));
				try {
					 result = (VCard) mXmppFacade.conllectVVPacket(new VVPacketAdapter(card)).getPacket();
					
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Packet result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(result !=null){
					VCard card = (VCard) result;
					Bitmap bitmap = null;
					if (card.getAvatar()!=null) {
						 bitmap = BitmapFactory.decodeByteArray(card.getAvatar(), 0, card.getAvatar().length);
						 myContact = new Contact(card.getTo());
						 Log.i("VV","onPostExecute "+ StringUtils.encodeBase64(card.getAvatar()));
						 myContact.setPhoto(StringUtils.encodeBase64(card.getAvatar()));
						 myContact.setName(card.getNickName());
						 //myCard = card;
						// myAvatar = bitmap;
					}
					if (bitmap!=null) {
						img.setImageBitmap(bitmap);
						chat_contact_name.setText(card.getNickName()==null?"未命名":card.getNickName());
					}else{
						img.setImageResource(R.drawable.no_avatar)	;
					}
				
				}else{
					img.setImageResource(R.drawable.no_avatar)	;
					}
		}
    	
    }
    //20120125
    
    public void onButtonClick( View v ) {
		
		com.butterfly.vv.view.clistmenu.QuickActionView qa = com.butterfly.vv.view.clistmenu.QuickActionView.Builder( v );
		
		qa.setAdapter( new com.butterfly.vv.view.clistmenu.CustomAdapter( this ) );
		qa.setNumColumns( (int) (1 + 0) );
		//qa.setOnClickListener( new MyDIACLic(v));
		qa.setOnClickListener( new DialogInterface.OnClickListener() {

			@Override
			public void onClick( DialogInterface dialog, int which ) {
				dialog.dismiss();
				//onDialogC((View) dialog);
				//listPopupItemSelected(which);
				Toast.makeText( getBaseContext(), "Selected item: " + which, Toast.LENGTH_SHORT ).show();
			}
		} );
		qa.show();
	}
    
    class VVContactsListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			//contactsListButtonSelected(v.getId());
		}
    	
    }
  //-----------------------------end---------------------------

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_expandable_main);
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.vv_title_main_bar);
		mXmppFacade = ContactListSessionSearchTab.getmXmppFacade();
		Log.i("VV", "+++"+mXmppFacade);
		//+++++++++++++++++++++++++++begin
		mSettings = PreferenceManager.getDefaultSharedPreferences(this);
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);//20130225
		//setContentView(R.layout.contactlist);
//		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.vv_title_main_bar);
		this.registerReceiver(mReceiver, new IntentFilter(BeemBroadcastReceiver.BEEM_CONNECTION_CLOSED));

//		viewPager = (ViewPager) findViewById(R.id.pager);
		groupsPagesAdapter = new ListPagerAdapter(getSupportFragmentManager(),new ViewPager(this));
//		pagerTabs = (PagerTabStrip) findViewById(R.id.tabstrip);
//		pagerTabs.setTabIndicatorColorResource(R.color.vert_manu);
//		pagerTabs.setNonPrimaryAlpha(PAGER_TAB_SECONDARY_ALPHA);

		mListGroup.add(getString(R.string.contact_list_all_contact));
		mListGroup.add(getString(R.string.contact_list_no_group));
//		groupsPagesAdapter.notifyDataSetChanged();
		/*
		Button add_contact_button = (Button) findViewById(R.id.add_contact_button);
		TextView contacts_textView2 = (TextView) findViewById(R.id.contacts_textView2);
		contacts_textView2.setText(getString(R.string.contact_list_name));
		add_contact_button.setOnClickListener(new VVContactsListener());
		
		chat_contact_name = (TextView) findViewById(R.id.chat_contact_name);
		 imageView = (RemoteImageView) findViewById(R.id.list_myself_status_icon);
		imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				modifyMyVcardClick();
				}
		});
		
		ImageView imageViewself = (ImageView) findViewById(R.id.vvselfzone);
		imageViewself.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Debug.getDebugInstance().log("enter myself zone ");
				com.teleca.jamendo.api.Contact contact = new com.teleca.jamendo.api.Contact();
				
				//contact.setmID("16");
				try {
					contact.setmID(VVXMPPUtils.getVvxmppUtils().makeVVXmppJid(mXmppFacade.getUserInfo().getJid()));
					Debug.getDebugInstance().log("current jid: "+mXmppFacade.getUserInfo().getJid()+" "+contact.getmID());
				} catch (RemoteException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				contactClickHandler(contact,mRoster);
				//AlbumListUI.launch( getParent(), contact);
			}
		});*/
		//}20121129
		//+++++++++++++++++++++++++++++++end
		
		
		
		initData();
		// mInflater = (LayoutInflater)
		// getSystemService(LAYOUT_INFLATER_SERVICE);
		for (Iterator iterator = mListGroup.iterator(); iterator.hasNext();) {
			String type = (String) iterator.next();
			Log.i("VV",mListGroup.size()+"====="+type+" ****"+getContactListAdapter(type).getCount());
			;
		}
		String group = mListGroup.get(0);
	   //(getContactListAdapter(group));
		//Log.i("VV",mListGroup.get(0)+"====="+mListGroup.get(1));
		expandAdapter = new ExpandableAdapter(ContactExpandableList.this);
		expandableList = (ExpandableListView) findViewById(R.id.list);
		View v = new View(this);
		expandableList.addHeaderView(v);
		// indicatorGroup = (LinearLayout) findViewById(R.id.topGroup);
		expandableList.setAdapter(expandAdapter);
		expandableList.setGroupIndicator(null);
		initView();
	}

	private void initData() {
		for (int i = 1; i <= 10; i++) {
			Map<String, String> curGroupMap = new HashMap<String, String>();
			groupData.add(curGroupMap);
			curGroupMap.put("group_text", "Group " + i);
			List<Map<String, String>> children = new ArrayList<Map<String, String>>();
			for (int j = 1; j < 5; j++) {
				Map<String, String> curChildMap = new HashMap<String, String>();
				children.add(curChildMap);
				curChildMap.put("child_text1", "Child " + j);
				curChildMap.put("child_text2", "Child " + j);
			}
			childData.add(children);
		}
	}

	public void initView() {
		/**
		 * 鐩戝惉鐖惰妭鐐规墦寮�殑浜嬩欢
		 */
		expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {
				the_group_expand_position = groupPosition;
				ids.put(groupPosition, groupPosition);
				count_expand = ids.size();
			}
		});
		/**
		 * 鐩戝惉鐖惰妭鐐瑰叧闂殑浜嬩欢
		 */
		expandableList
				.setOnGroupCollapseListener(new OnGroupCollapseListener() {
					@Override
					public void onGroupCollapse(int groupPosition) {
						ids.remove(groupPosition);
						expandableList.setSelectedGroup(groupPosition);
						count_expand = ids.size();
					}
				});
		view_flotage = (LinearLayout) findViewById(R.id.topGroup);
		view_flotage.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				view_flotage.setVisibility(View.GONE);
				expandableList.collapseGroup(the_group_expand_position);
				expandableList.setSelectedGroup(the_group_expand_position);
			}
		});
		group_content = (TextView) findViewById(R.id.content_001);
		tubiao = (ImageView) findViewById(R.id.tubiao);
		tubiao.setBackgroundResource(R.drawable.btn_browser2);
		//璁剧疆婊氬姩浜嬩欢
		expandableList.setOnScrollListener(this);
		
		//20130226{
		expandableList.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				//Log.i("VV", groupPosition+"-----"+childPosition);
				return false;
			}
		});
		//20130226}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		//闃叉涓夋槦,榄呮棌绛夋墜鏈虹涓�釜鏉＄洰鍙互涓�洿寰�笅鎷�鐖舵潯鐩拰鎮诞鍚屾椂鍑虹幇鐨勯棶棰�
		if(firstVisibleItem==0){
			view_flotage.setVisibility(View.GONE);
		}
		// 鎺у埗婊戝姩鏃禩extView鐨勬樉绀轰笌闅愯棌
		int npos = view.pointToPosition(0, 0);
		if (npos != AdapterView.INVALID_POSITION) {
			long pos = expandableList.getExpandableListPosition(npos);
			int childPos = ExpandableListView.getPackedPositionChild(pos);
			final int groupPos = ExpandableListView.getPackedPositionGroup(pos);
			if (childPos == AdapterView.INVALID_POSITION) {
				View groupView = expandableList.getChildAt(npos
						- expandableList.getFirstVisiblePosition());
				indicatorGroupHeight = groupView.getHeight();
			}
			
			if (indicatorGroupHeight == 0) {
				return;
			}
			// if (isExpanded) {
			if (count_expand > 0) {
				the_group_expand_position = groupPos;
				group_content.setText(groupData.get(the_group_expand_position)
						.get("group_text"));
				if (the_group_expand_position != groupPos||!expandableList.isGroupExpanded(groupPos)) {
					view_flotage.setVisibility(View.GONE);
				} else {
					view_flotage.setVisibility(View.VISIBLE);
				}
			}
			if (count_expand == 0) {
				view_flotage.setVisibility(View.GONE);
			}
		}

		if (the_group_expand_position == -1) {
			return;
		}
		/**
		 * calculate point (0,indicatorGroupHeight)
		 */
		int showHeight = getHeight();
		MarginLayoutParams layoutParams = (MarginLayoutParams) view_flotage
				.getLayoutParams();
		// 寰楀埌鎮诞鐨勬潯婊戝嚭灞忓箷澶氬皯
		layoutParams.topMargin = -(indicatorGroupHeight - showHeight);
		view_flotage.setLayoutParams(layoutParams);
	}

	class ExpandableAdapter extends BaseExpandableListAdapter {
		ContactExpandableList exlistview;
		@SuppressWarnings("unused")
		private int mHideGroupPos = -1;

		public ExpandableAdapter(ContactExpandableList elv) {
			super();
			exlistview = elv;
		}

		// **************************************
		public Object getChild(int groupPosition, int childPosition) {
			return childData.get(groupPosition).get(childPosition)
					.get("child_text1").toString();
		}

		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		public int getChildrenCount(int groupPosition) {
			return childData.get(groupPosition).size();
		}

		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				//view = inflater.inflate(R.layout.childitem, null);
				view = inflater.inflate(R.layout.contactlistcontact, null);
			}
//			final TextView title = (TextView) view
//					.findViewById(R.id.child_text);
//			title.setText(childData.get(groupPosition).get(childPosition)
//					.get("child_text1").toString());
//			final TextView title2 = (TextView) view
//					.findViewById(R.id.child_text2);
//			title2.setText(childData.get(groupPosition).get(childPosition)
//					.get("child_text2").toString());
			return view;
		}

		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			View view = convertView;
			if (view == null) {
				LayoutInflater inflaterGroup = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflaterGroup.inflate(R.layout.groupitem, null);
			}
			TextView title = (TextView) view.findViewById(R.id.content_001);
			title.setText(getGroup(groupPosition).toString());
			ImageView image = (ImageView) view.findViewById(R.id.tubiao);

			//System.out.println("isExpanded----->" + isExpanded);
			if (isExpanded) {
				image.setBackgroundResource(R.drawable.btn_browser2);
			} else {
				image.setBackgroundResource(R.drawable.btn_browser);
			}
			return view;
		}

		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		public Object getGroup(int groupPosition) {
			return groupData.get(groupPosition).get("group_text").toString();
		}

		public int getGroupCount() {
			return groupData.size();

		}

		public boolean hasStableIds() {
			return true;
		}

		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}

		public void hideGroup(int groupPos) {
			mHideGroupPos = groupPos;
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		return super.dispatchTouchEvent(ev);
	}

	private int getHeight() {
		int showHeight = indicatorGroupHeight;
		int nEndPos = expandableList.pointToPosition(0, indicatorGroupHeight);
		if (nEndPos != AdapterView.INVALID_POSITION) {
			long pos = expandableList.getExpandableListPosition(nEndPos);
			int groupPos = ExpandableListView.getPackedPositionGroup(pos);
			if (groupPos != the_group_expand_position) {
				View viewNext = expandableList.getChildAt(nEndPos
						- expandableList.getFirstVisiblePosition());
				showHeight = viewNext.getTop();
			}
		}
		return showHeight;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}
}
